<?php
$title = 'Gestion des budgets';
$this->headTitle($title);
$this->headLink()->prependStylesheet($this->basePath()."/css/operationtest/bootstrap-editable.css");
$this->headScript()->prependFile($this->basePath()."/js/operationtest/moment-with-langs.min.js");
$this->headScript()->prependFile($this->basePath()."/js/operationtest/bootstrap-editable.min.js");
?>

<script type="text/javascript">
    $(function(){
        // Manage Uri
        var uri = document.location.pathname;
        var operationtestPos = uri.lastIndexOf("operationtest");
        var resteUri = uri.substring(operationtestPos,100);
        var operationtestUri = uri.replace(resteUri, "operationtest");
        var publicUri = uri.replace(operationtestUri, "");
        
        moment.lang('fr');
        
        // Show budget for this month
        var date = new Date();
        var m;
        if(date.getMonth() < 10) {
            m = '0'+(date.getMonth()+1);
        } else {
            m = date.getMonth()+1;
        }
        var thisMonth = date.getFullYear()+'-'+m+'-01';
        getMonthBudget(thisMonth);
        
        //editables 
        $.fn.editable.defaults.mode = 'popup';  
        $.fn.editable.defaults.emptytext = 'Aucun'; 
        $.fn.editable.defaults.url = uri; 
        $.fn.editable.defaults.placement = 'bottom';
        
        $('.editable').editable();
        
        $('#a-month-budget').editable({
            placement: 'bottom',
            format: 'YYYY-MM-DD',    
            viewformat: 'MMMM YYYY',    
            template: 'MMMM YYYY',
            combodate: {
                minYear: 2000,
                maxYear: 2030,
                minuteStep: 1
            },
            validate: function(v){
                if(!v) {
                    return 'Mois incorrect';
                } else {
                    var monthObject = moment(v);
                    var month = (monthObject.format('YYYY-MM-DD'));
                    set_opacity('budget-month-container', 10);
                    getMonthBudget(month);
                }
            }
        }); 
    });
    
    function getMonthBudget(month) {
        // Manage Uri
        var uri = document.location.pathname;
        var operationtestPos = uri.lastIndexOf("operationtest");
        var resteUri = uri.substring(operationtestPos,100);
        var operationtestUri = uri.replace(resteUri, "operationtest");
        var publicUri = uri.replace(operationtestUri, ""); 
        
        $('#thisMonth').val(month);
        $("#budget-month-container .editable").editable('option','disabled',true);
        
        $.ajax({
            type: 'POST',
            url: operationtestUri+'/budget/getmonthbudget',
            dataType: 'html',
            cache: false,
            data: { month: month },
            success: function(contenu) {
                var requestDate = new Date(month);
                var currentDate = new Date();
                var diff = currentDate.getTime() - requestDate.getTime();
                var desactive = false;
                
                if(diff >= 0) {
                    desactive = true;
                }
                
                set_opacity('budget-month-container', 100);
                $('#budget-month-container').empty();
                $('#budget-month-container').html(contenu); 
                $("#budget-month-container .editable").editable('option','disabled',desactive);
                $('#budget-month-container').fadeIn('slow'); 
            }
        });
    }
    
    function set_opacity(id, opacity) {
        var el = document.getElementById(id);
        el.style["filter"] = "alpha(opacity="+opacity+")";
        el.style["-moz-opacity"] = opacity/100;
        el.style["-khtml-opacity"] = opacity/100;
        el.style["opacity"] = opacity/100;
        return true;
    }
</script>

<div class="content-head">
    <h3><?php echo $title; ?> du mois de 
        <b><a id="a-month-budget"
            data-name="month"
            data-type="combodate"
            data-value="<?php echo Date('Y-m-01'); ?>"></a></b>
        <img src="<?php echo $this->basePath().'/img/operationtest/refresh-16.png'; ?>" alt="Actualiser" title="Actualiser les données" style="cursor:pointer" onclick="set_opacity('budget-month-container', 10); getMonthBudget($('#thisMonth').val());" />
    </h3>
</div>
<div id="budget-month-container"></div>
<input type="hidden" id="thisMonth" />